package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;
import java.text.DecimalFormat;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OrderActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView Tv_Name,Tv_Price,Tv_quantity,Tv_Prices;
    private Button Btn_sub,Btn_add,Btn_Yes,Btn_No;
    private int num;
    private static final String TAG = "OrderActivity";
    private int uid;
    private Double UnitPrice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);

        Tv_Name = findViewById(R.id.Tv_Name);
        Tv_Price = findViewById(R.id.Tv_Price);
        Tv_quantity = findViewById(R.id.Tv_quantity);
        Tv_Prices = findViewById(R.id.Tv_Prices);

        Btn_sub = findViewById(R.id.Btn_sub);
        Btn_add = findViewById(R.id.Btn_add);
        Btn_Yes = findViewById(R.id.Btn_Yes);

        Btn_sub.setOnClickListener(this);
        Btn_add.setOnClickListener(this);
        Btn_Yes.setOnClickListener(this);


        Intent intent = getIntent();
        uid = intent.getIntExtra("uid",0);


        show();
    }


    private void show() {
        new Thread(new Runnable() {
            private static final String TAG = "MainActivity";
            @Override
            public void run() {
                //1 client对象
                OkHttpClient client = new OkHttpClient();
                //2 Request对象
                FormBody body = new FormBody.Builder()

                        .build();
                Request request = new Request.Builder()
                        .url("http://10.2.64.68:8080/juyuan/foods/getFoods?id="+uid)
                        .post(body)
                        .build();
                //3 call对象
                final Call call = client.newCall(request);
                //4 执行call对象得到数据
                try {
                    Response response = call.execute();
                    final String data = response.body().string();
                    Gson gson = new Gson();
                    getFoodGson gu = gson.fromJson(data, getFoodGson.class);

                    UnitPrice = Double.valueOf(gu.getPrice());
                    Log.e(TAG, "run: "+UnitPrice );

                    Tv_Name.setText(gu.getName());
                    Tv_Price.setText(gu.getPrice() +"元");



                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }).start();

    }
    @Override
    public void onClick(View v) {
        DecimalFormat df = new DecimalFormat("#.00");
        switch (v.getId()){
            case R.id.Btn_sub:
                num = num - 1;

                if (num >= 0){
                Tv_quantity.setText(num + "");

                Tv_Prices.setText((df.format(num * UnitPrice)) + "元");
            }else{
                    num = 0;
                }
                break;
            case R.id.Btn_add:
                num = num + 1;
                Tv_quantity.setText(num+"");
                Log.e(TAG, "onClick: "+ num * UnitPrice);
                Tv_Prices.setText(((df.format(num * UnitPrice)) +"元"));
                break;
            case R.id.Btn_Yes:
                Toast.makeText(this, "订购成功！", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}